/*
 * @Description:
 * @Author: r_yuesheng
 * @Date: 2022-03-30 16:31:00
 * @LastEditTime: 2022-03-30 18:44:41
 * @LastEditors: r_yuesheng
 */
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';

const loadMovieAPI = () => {
  return fetch(
    'https://pcw-api.iqiyi.com/search/recommend/list?channel_id=1&data_type=1&mode=11&page_id=1&ret_num=48&session=0d5d98d2fd37b7a71914a441c7c3b929'
  ).then(res => res.json());
};

// 此action可以直接调用，有三种状态
export const loadData = createAsyncThunk('async/loadData', async () => {
  const res = await loadMovieAPI();
  return res;
});

const initialState = {
  listData: []
};

export const asyncSlice = createSlice({
  name: 'async',
  initialState,
  reducers: {
    // add: state => {
    //   state.value += 1;
    // }
  },
  extraReducers: {
    [loadData.fulfilled](state, { payload }) {
      console.log('成功了');
      state.listData = payload.data.list;
      console.log(payload.data.list);
    },
    [loadData.pending](state) {
      console.log('加载中');
    }
  }
});

// Action creators are generated for each case reducer function
// export const { add, del } = asyncSlice.actions;

export default asyncSlice.reducer;
